/*action create by pionner code builder*/
package com.gotop.sms.fysms;

import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;

import com.app.util.DateTimeUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.dev.osuser.user.UserlistDAO;

public class UserBacterinAction implements Action, Serializable {

	// key
	private java.lang.Integer id;
	private String code;
	private java.lang.Integer[] ids;
	private UserBacterin userbacterin;
	private java.lang.Integer userId;
	private java.lang.String userName;
	private String birthday;
	private java.lang.Integer bacterinId;
	private java.util.Date orderTime;
	private java.util.Date injectTime;
	private List objects=null;
	private String condition=null;
	private String rtpage=null;
	private String fromTree=null;
	private String message=null;
	
	/** 默认构造方法 */
	public UserBacterinAction(){
	}
	/** full constructor */
	public UserBacterinAction(java.lang.Integer userId,java.lang.Integer bacterinId,java.util.Date orderTime,java.util.Date injectTim){
		this.userId=userId;
		this.bacterinId=bacterinId;
		this.orderTime=orderTime;
		this.injectTime=injectTime;

	}
	public void setId(java.lang.Integer id){
		this.id=id;
	}
	public java.lang.Integer getId(){
		return id;
	}

	public void setIds(java.lang.Integer[] ids){
		this.ids=ids;
	}
	public java.lang.Integer[] getIds(){
		return ids;
	}

	public void setUserBacterin(UserBacterin userbacterin){
		this.userbacterin=userbacterin;
	}
	public UserBacterin getUserBacterin(){
		return userbacterin;
	}

	public void setUserId(java.lang.Integer userId){
		this.userId=userId;
	}
	public java.lang.Integer getUserId(){
		return userId;
	}

	public void setBacterinId(java.lang.Integer bacterinId){
		this.bacterinId=bacterinId;
	}
	public java.lang.Integer getBacterinId(){
		return bacterinId;
	}

	public void setOrderTime(java.util.Date orderTime){
		this.orderTime=orderTime;
	}
	public java.util.Date getOrderTime(){
		return orderTime;
	}

	public void setInjectTime(java.util.Date injectTime){
		this.injectTime=injectTime;
	}
	public java.util.Date getInjectTime(){
		return injectTime;
	}

	public List getObjects() {
		return objects;
	}

	public void setObjects(List objects) {
		this.objects = objects;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String execute() throws Exception {
		return "SUCESS";
	}

	public String doList(){
		if(null==this.userId){
			this.userId=(Integer)ActionContext.getContext().getSession().get("myUserId");
		}
		UserBacterinDAO dao=new UserBacterinDAO();
		UserlistDAO userDao=new UserlistDAO();
		Userlist user=userDao.findById(this.userId);
		if(null!=user){
			this.userName=user.getCname();
			this.birthday=DateTimeUtil.getDateTime(user.getBirthday(), "yyyy年 MM月 dd日");
//			this.birthday=user.getBirthday();
		}
		this.objects=dao.findByCondition("userId="+this.userId);
		if(null==this.objects || this.objects.size()<1){
			//初始化用户防疫轨迹信息 
			BacterinService bs=new BacterinService();
			bs.userBacterinInit(userId);
			this.objects=dao.findByCondition("userId="+this.userId);
		}
		if(null!=this.objects){
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			String bName=null;
			
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo)
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
			}
		}
		
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setHeader("Pragma","No-cache"); 
		response.setHeader("Cache-Control","no-cache"); 
		response.setDateHeader("Expires", -10); 
		return "list";
	}
	public String doUserView(){
		if(null==this.userId){
			this.userId=(Integer)ActionContext.getContext().getSession().get("myUserId");
		}
		UserBacterinDAO dao=new UserBacterinDAO();
		UserlistDAO userDao=new UserlistDAO();
		Userlist user=userDao.findById(this.userId);
		if(null!=user){
			this.userName=user.getCname();
			this.birthday=DateTimeUtil.getDateTime(user.getBirthday(), "yyyy年 MM月 dd日");
//			this.birthday=user.getBirthday();
		}
		this.objects=dao.findByCondition("userId="+this.userId);
		if(null==this.objects || this.objects.size()<1){
			//初始化用户防疫轨迹信息 
			BacterinService bs=new BacterinService();
			bs.userBacterinInit(userId);
			this.objects=dao.findByCondition("userId="+this.userId);
		}
		if(null!=this.objects){
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			String bName=null;
			
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo)
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
			}
		}
		return "list";
	}
	public String doUCodeView(){
		if(null==rtpage){
			rtpage="list";
		}
		if(null==this.code){
			this.userId=(Integer)ActionContext.getContext().getSession().get("mycode");
		}
		UserBacterinDAO dao=new UserBacterinDAO();
		UserlistDAO userDao=new UserlistDAO();
		Userlist user=userDao.findByCode(this.code);
		if(null!=user){
			this.userName=user.getCname();
			this.birthday=DateTimeUtil.getDateTime(user.getBirthday(), "yyyy年 MM月 dd日");
//			this.birthday=user.getBirthday();
		}else{
			this.message="没有检索到资料！";
			return rtpage;
		}
		this.objects=dao.findByCondition("userId="+user.getId());
		if(null==this.objects || this.objects.size()<1){
			//初始化用户防疫轨迹信息 
			BacterinService bs=new BacterinService();
			bs.userBacterinInit(user.getId());
			this.objects=dao.findByCondition("userId="+user.getId());
		}
		if(null!=this.objects){
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			String bName=null;
			
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo)
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
			}
		}
		
		return rtpage;
	}

	public String doAdd(){
		return "add";
	}
	public String doExport(){
		System.out.println(this.userId);
		UserBacterinDAO dao=new UserBacterinDAO();
		this.objects=dao.findByCondition("userId="+this.userId);
		if(null!=this.objects){
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			String bName=null;
			
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo)
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
			}
		}
		try {
			HttpServletResponse response = ServletActionContext.getResponse();

			response.setContentType("application/vnd.ms-excel");
			response.addHeader("Content-Disposition",
					"attachment;filename=Bacterin.xls");
			OutputStream os = response.getOutputStream();

			WritableWorkbook workbook = Workbook.createWorkbook(os);// 创建工作薄

			WritableSheet worksheet = workbook.createSheet("record", 0);// 创建第一个工作表，name:工作表名称

			Label label = null;// 用于写入文本内容到工作表中去

			// 开始写入第一行，即标题栏
			String[] title = { "编号", "疫苗名称", "疫苗类别", "预约时间", "接种时间",
					"备注"};
			for (int i = 0; i < title.length; i++) {

				label = new Label(i, 0, title[i]);// 参数依次代表列数、行数、内容

				worksheet.addCell(label);// 写入单元格

			}

			// 开始写入内容
			for (int i = 0; i < objects.size(); i++) {

				UserBacterin ul=(UserBacterin)objects.get(i);//获取一条记录
				SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy-MM-dd");  	
				label = new Label(0, i + 1, i+1+"");
				worksheet.addCell(label);
				label = new Label(1, i + 1, ul.getBacterinName());
				worksheet.addCell(label);
				label = new Label(2, i + 1, ul.getType());
				worksheet.addCell(label);
				label = new Label(3, i + 1, formatter.format(ul.getOrderTime()));
				worksheet.addCell(label);
				if(ul.getInjectTime()!=null){
				label = new Label(4, i + 1, formatter.format(ul.getInjectTime()));	
				}else{
					label = new Label(4, i + 1, "");	
				}
				worksheet.addCell(label);
				label = new Label(5, i + 1, "");
				worksheet.addCell(label);		
			}
			workbook.write();
			workbook.close();			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return "list";
	}

	public String doEdit(){
		UserBacterinDAO dao=new UserBacterinDAO();
		this.userbacterin=dao.findById(id);
		this.sedObj();;
		return "edit";
	}

	public String doSave(){
		Transaction tx=null;
		try {
			this.buildObj();
			UserBacterinDAO dao=new UserBacterinDAO();
			tx=dao.getSession().beginTransaction();
			if(null==this.userbacterin.getId()){
//			insert a new object
			//gave the object id by database 
				dao.add(userbacterin);
				tx.commit();
			}else{
//			update the object
//				dao.getSession().clear();
				ActionContext.getContext().getSession().put("myUserId", this.userbacterin.getUserId());
				dao.update(userbacterin);				
				tx.commit();
				BacterinService bservice=new BacterinService();
				bservice.userBacterinfresh(userbacterin.getUserId());
				dao.getSession().clear();
			}
			
			dao.getSession().clear();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return "error";
		}
		
		return "list";
	}

	public String doDetail(){
			UserBacterinDAO dao=new UserBacterinDAO();
		this.userbacterin=dao.findById(id);
		this.sedObj();;
		if(null!=fromTree){;
			return "detail";
		}else{
			return "list";
		}
	}

	public String doDelete(){
		try{
			UserBacterinDAO dao=new UserBacterinDAO();
			dao.deleteObjs(ids);
			return "list";
		}catch(Exception e){
			return "error";
		}
	}

	private void buildObj(){
		if(null!=this.id){
			UserBacterinDAO dao=new UserBacterinDAO();
			this.userbacterin=dao.findById(id);
		}else{
			this.userbacterin=new UserBacterin();
			this.userbacterin.setId(this.id);
		}
		if(null!=this.userId)
			this.userbacterin.setUserId(this.userId);
		if(null!=this.bacterinId)
			this.userbacterin.setBacterinId(this.bacterinId);
		if(null!=this.orderTime)
			this.userbacterin.setOrderTime(this.orderTime);
		if(null!=this.injectTime)
		this.userbacterin.setInjectTime(this.injectTime);
	}
	private void sedObj(){

		if(null!=this.userbacterin){
			this.id=this.userbacterin.getId();
			this.userId=this.userbacterin.getUserId();
			this.bacterinId=this.userbacterin.getBacterinId();
			this.orderTime=this.userbacterin.getOrderTime();
			this.injectTime=this.userbacterin.getInjectTime();

		}
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getRtpage() {
		return rtpage;
	}
	public void setRtpage(String rtpage) {
		this.rtpage = rtpage;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}	
}